package com.qipay.admin.doc.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.doc.vo.ActionDocDetailDTO;
import com.qipay.baas.model.ActionDoc;
import com.qipay.baas.model.ActionDocDetail;
import com.qipay.doc.service.ActionDocService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotNull;
import java.util.Date;

@Controller
@RequestMapping("/doc")
public class DocController extends BaseController {

    @Autowired
    ActionDocService actionDocService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(@RequestParam(value = "t", required = false, defaultValue = "1") byte type) {
        ModelAndView view = createModelAndView("doc-index-lst");
        view.addObject("lst", actionDocService.selectActionDocByType(type));
        return view;
    }

    @RequestMapping(value = "/child", method = RequestMethod.GET)
    public ModelAndView indexChild(@RequestParam(value = "did", required = false, defaultValue = "0") int did) {
        ModelAndView view = createModelAndView("doc-child-lst");
        view.addObject("lst", actionDocService.selectActionDocDetailByDocId(did));
        return view;
    }

    @RequestMapping(value = {"/{id:[\\d]+}"}, method = RequestMethod.GET)
    public ModelAndView detail(@PathVariable @NotNull int id) {
        ModelAndView view = createModelAndView("doc-index-detail");
        view.addObject("detail", actionDocService.selectActionDocByPK(id));
        view.addObject("isadd", "0");
        return view;
    }

    @RequestMapping(value = {"/add"}, method = RequestMethod.GET)
    public ModelAndView detailAdd() {
        ModelAndView view = createModelAndView("doc-index-detail");
        view.addObject("isadd", "1");
        return view;
    }

    @RequestMapping(value = {"/child/{id:[\\d]+}"}, method = RequestMethod.GET)
    public ModelAndView detailChild(@PathVariable @NotNull int id) {
        ModelAndView view = createModelAndView("doc-child-detail");
        view.addObject("lst", actionDocService.selectActionDocByType((byte) 2));
        view.addObject("detail", actionDocService.selectActionDocDetailByPK(id));
        view.addObject("isadd", "0");
        return view;
    }

    @RequestMapping(value = {"/child/add"}, method = RequestMethod.GET)
    public ModelAndView detailAdd(@RequestParam(value = "did", required = false, defaultValue = "0") int did) {
        ModelAndView view = createModelAndView("doc-child-detail");
        view.addObject("lst", actionDocService.selectActionDocByType((byte) 2));
        view.addObject("isadd", "1");
        return view;
    }

    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    public String save(ActionDoc actionDoc) {
        if (actionDoc != null && actionDoc.getId() != null && actionDoc.getId() > 0) {
            ActionDoc db = actionDocService.selectActionDocByPK(actionDoc.getId());
            if (db != null) {
                BeanUtils.copyProperties(actionDoc, db);
                actionDocService.updateActionDoc(db);
            }
        } else {
            actionDocService.insertActionDoc(actionDoc);
        }
        return "redirect:/doc?t="+actionDoc.getType();
    }

    @RequestMapping(value = {"/child/save"}, method = RequestMethod.POST)
    public String saveChild(ActionDocDetailDTO actionDoc) {
        if (actionDoc != null && actionDoc.getId() != null && actionDoc.getId() > 0) {
            ActionDocDetail db = actionDocService.selectActionDocDetailByPK(actionDoc.getId());
            if (db != null) {
                BeanUtils.copyProperties(actionDoc, db);
                actionDocService.updateActionDocDetail(db);
            }
        } else {
            ActionDocDetail db = new ActionDocDetail();
            BeanUtils.copyProperties(actionDoc, db);
            db.setCreateTime(new Date());
            actionDocService.insertActionDocDetail(db);
        }
        return "redirect:/doc/child?did=" + actionDoc.getDocId();
    }
}
